package com.smedi.ismedi.authoritymanagement.core.application.service.impl;

import com.smedi.ismedi.authoritymanagement.core.application.command.RoleBasicCommand;
import com.smedi.ismedi.authoritymanagement.core.application.service.RoleBasicApplicationService;
import com.smedi.ismedi.authoritymanagement.core.domain.aggregatemodel.RoleBasic;
import com.smedi.ismedi.authoritymanagement.core.domain.service.RoleBasicDomainService;
import com.smedi.ismedi.authoritymanagement.core.domain.service.RoleMemberDomainService;
import com.smedi.ismedi.authoritymanagement.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.authoritymanagement.core.port.adapter.service.PersonnelRemoteApiService;
import com.smedi.ismedi.web.starter.exception.LogicException;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xulinjun
 * @Date: 2021-8-10 17:51
 */
@Slf4j
@Service
@AllArgsConstructor
public class RoleBasicApplicationServiceImpl implements RoleBasicApplicationService {
    private final DozerUtil dozerUtil;

    private final RoleBasicDomainService roleBasicDomainService;
    private final PersonnelRemoteApiService personnelRemoteApiService;
    private final RoleMemberDomainService roleMemberDomainService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RoleBasic saveRoleBasic(RoleBasicCommand roleBasicCommand) {
        RoleBasic roleBasic = dozerUtil.convertor(roleBasicCommand, RoleBasic.class);
        return roleBasicDomainService.saveRoleBasic(roleBasic);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<RoleBasic> batchSaveRoleBasic(List<RoleBasicCommand> roleBasicCommands) {
        List<RoleBasic> list = new ArrayList<>();
        for (RoleBasicCommand roleBasicCommand : roleBasicCommands) {
            list.add(this.saveRoleBasic(roleBasicCommand));
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRoleBasic(Long roleId) {
        roleBasicDomainService.deleteRoleBasicByRoleId(roleId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDeleteRoleBasics(List<Long> roleIds) {
        for (Long roleId : roleIds) {
            deleteRoleBasic(roleId);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchSaveOneRoleForMultiPerson(Long roleId, List<Long> personIds) {
        roleBasicDomainService.getRoleBasicByRoleId(roleId); // 校验当前角色是否存在
        if (personIds.size() <= 0) {
            throw LogicException.raise(CustomError.PERSON_ID_EMPTY);
        } else {
            for (Long personId : personIds) {
                if (!personnelRemoteApiService.checkPersonIsAvailable(personId)) {
                    throw LogicException.raise(CustomError.PERSON_NOT_AVAILABLE);
                } else {
                    // 保存分配信息
                    roleMemberDomainService.saveRoleMember(roleId, personId);
                }
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchSaveMultiRoleForOnePerson(List<Long> roleIds, Long personId) {
        if (roleIds.size() <= 0) {
            throw LogicException.raise(CustomError.ROLE_ID_EMPTY);
        } else {
            if (!personnelRemoteApiService.checkPersonIsAvailable(personId)) {
                throw LogicException.raise(CustomError.PERSON_NOT_AVAILABLE);
            } else {
                for (Long roleId : roleIds) {
                    roleMemberDomainService.saveRoleMember(roleId, personId);
                }
            }
        }
    }

    @Override
    public void deleteRoleMemberByRoleMemberId(Long roleMemberId) {
        roleMemberDomainService.deleteRoleMemberByRoleMemberId(roleMemberId);
    }
}
